//
//  BVMyDiscountCell.m
//  BuyVegetables1
//
//  Created by 叶思超 on 15/10/23.
//  Copyright (c) 2015年 叶思超. All rights reserved.
//

#import "BVMyDiscountCell.h"
#import "BVConst.h"
#import "UIView+Extension.h"
#import "BVMyDiscount.h"

@interface BVMyDiscountCell ()

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

@property (weak, nonatomic) IBOutlet UILabel *userfulTimeLabel;

@property (weak, nonatomic) IBOutlet UILabel *moneyLabel;

/**是否使用过*/
@property (weak, nonatomic) IBOutlet UILabel *useLabel;

/**是否过期*/
@property (weak, nonatomic) IBOutlet UILabel *expireLabel;


@property (weak, nonatomic) IBOutlet NSLayoutConstraint *chooseBtnConstraint;

@property (weak, nonatomic) IBOutlet UIImageView *decorateImgView;


@end

@implementation BVMyDiscountCell

+ (instancetype)cellWithTableView:(UITableView *)tableView {

    [tableView registerNib:[UINib nibWithNibName:@"BVMyDiscountCell" bundle:nil] forCellReuseIdentifier:NSStringFromClass([self class])];
    return [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([self class])];
    
}

- (void)setMyDiscount:(BVMyDiscount *)myDiscount{
    _myDiscount = myDiscount;
    
    //金钱
    NSMutableAttributedString * attrMoney = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"￥%@",_myDiscount.couponMoney]];
    [attrMoney addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:NSMakeRange(0, 1)];
    self.moneyLabel.attributedText = attrMoney;
    
    //名称
    NSMutableAttributedString * titleAttr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@(满%@元可用)",_myDiscount.couponName,_myDiscount.condition]];
    [titleAttr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:NSMakeRange(_myDiscount.couponName.length, titleAttr.length - _myDiscount.couponName.length)];
    self.titleLabel.attributedText = titleAttr;
    
    //有效期
    self.userfulTimeLabel.text = _myDiscount.usefultime;
    
    self.useLabel.text = _myDiscount.status.integerValue ? @"已使用" : @"未使用";
    self.expireLabel.text = _myDiscount.coupon_expired.integerValue ? @"已过期" : @"未过期";
    //是否有效
    if (!_myDiscount.coupon_expired.integerValue && _myDiscount.isAvailable && !_myDiscount.status.integerValue) {
        self.chooseBtn.enabled = YES;
    }else{
       self.chooseBtn.enabled = NO; 
    }
    if(_myDiscount.coupon_expired.integerValue || !_myDiscount.isAvailable || _myDiscount.status.integerValue){
        
        self.titleLabel.textColor = [UIColor lightGrayColor];
        self.userfulTimeLabel.textColor = [UIColor lightGrayColor];
        self.moneyLabel.textColor = [UIColor lightGrayColor]
        ;
        self.decorateImgView.image = [UIImage imageNamed:@"wdyhq_coupons_used"];
        self.useLabel.textColor = [UIColor lightGrayColor];
        self.expireLabel.textColor = [UIColor lightGrayColor];
    }else{
        self.titleLabel.textColor = [UIColor blackColor];
        self.userfulTimeLabel.textColor = [UIColor blackColor];
        self.moneyLabel.textColor = BVBaseOrangeColor;
        self.decorateImgView.image = [UIImage imageNamed:@"wdyhq_coupons"];
        self.useLabel.textColor = [UIColor blackColor];
        self.expireLabel.textColor = [UIColor blackColor];
    }
}

//选择优惠券
- (IBAction)chooseBtnClicked:(id)sender {
    
    UIButton * btn = sender;
    btn.selected = !btn.isSelected;
    self.selected = btn.isSelected;
    [self.delegate discountCell:self didSelectedDsicont:self.myDiscount];
}

- (void)setShowChooseBtn:(BOOL)showChooseBtn{

    self.chooseBtnConstraint.constant = showChooseBtn ? 15: 0;
}

@end
